/**
 * Created by lixiaodi on 2019/03/15.
 */
mini.parse();
//得到密码
var newPasswordTxt = mini.get('newPassword');
//页面初始化
$(function(){
	//$('#errorAlert').show();
	//密码强弱度
	$('#newPassword').keyup(function () {
        var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
        var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
        var enoughRegex = new RegExp("(?=.{6,}).*", "g");
        if (false == enoughRegex.test(newPasswordTxt.getValue())) {
            $('#level').removeClass('pw-weak');
            $('#level').removeClass('pw-medium');
            $('#level').removeClass('pw-strong');
            $('#level').addClass(' pw-defule');
            //密码小于六位的时候，密码强度图片都为灰色
        }
        else if (strongRegex.test(newPasswordTxt.getValue())) {
            $('#level').removeClass('pw-weak');
            $('#level').removeClass('pw-medium');
            $('#level').removeClass('pw-strong');
            $('#level').addClass(' pw-strong');
            //密码为八位及以上并且字母数字特殊字符三项都包括,强度最强
        }
        else if (mediumRegex.test(newPasswordTxt.getValue())) {
            $('#level').removeClass('pw-weak');
            $('#level').removeClass('pw-medium');
            $('#level').removeClass('pw-strong');
            $('#level').addClass(' pw-medium');
            //密码为七位及以上并且字母、数字、特殊字符三项中有两项，强度是中等
        }
        else {
            $('#level').removeClass('pw-weak');
            $('#level').removeClass('pw-medium');
            $('#level').removeClass('pw-strong');
            $('#level').addClass('pw-weak');
            //如果密码为6为及以下，就算字母、数字、特殊字符三项都包括，强度也是弱的
        }
        return true;
    });
});
//校验旧密码是否正确
function oldParsseord(){
	var outPassword = mini.get("outPassword").getValue();
	$.ajax({
		url : ajaxUrl + '/user/findPassword.do',
		type : 'POST',
		data : {
			password : outPassword
		},
		success : function(data){
			if(data.state == "error"){
				$('#errorAlert').show();
			}else{
				$('#errorAlert').hide();
			}
		},
		error : function(data){
			console.log(data);
		}
	})
}
//保存
function save(){
	//三次输入密码的内容
	var outPassword = mini.get("outPassword").getValue();
	var newPassword = mini.get("newPassword").getValue();
	var newPassword2 = mini.get("newPassword2").getValue();
	//校验
	if(outPassword){
		if(newPassword){
			if(newPassword2){
				//两次新密码输入必须一致判断
				if(newPassword != newPassword2){
					mini.alert("输入新密码不一致，请核对后提交");
				}else{
					var url = ajaxUrl + "/user/updateLoginNewPassWord.do";
					//ajaxUrl + "/user/saveUser.do",
					$.ajax({
						type : 'POST',
						url : url,
						dataType : 'json',
						data: {
							password : outPassword,
							newPassWord : newPassword2
						},
						success: function(data){
							if(data.message != "success"){
								mini.alert("密码修改失败");
							}else{
								mini.alert("密码修改成功");

							}
							CloseWindow("save")
						},
						error :function(message){
							mini.alert(message);
						}
					})
				}
			}else{
				mini.alert('请输入第二遍新密码');
			}
		}else{
			mini.alert('请输入新密码');
		}
	}else{
		mini.alert('请输入旧密码');
	}
}
//关闭弹出页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
	save();
}
function onCancel(e) {
    CloseWindow("cancel");
}